
//查询
function query(type) {
	var xm = $('#xm').val();
	var sfzhm = $('#sfzhm').val();
	if(xm == ''){
		alert("请输入姓名");
		$('#xm').focus();
		return;
	}
	if(xm.length > 10){
		alert("姓名长度超过限制，请重新输入！");
		$('#xm').focus();
		return;
	}
	if(sfzhm == ''){
		alert("请输入身份证号");
		$('#sfzhm').focus();
		return;
	}
	var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	if(reg.test(sfzhm) === false) { 
		alert("身份证输入不合法，请重新输入！");
		$('#sfzhm').focus();
		return;
	}
    var url = '../api/wjw/gateway/httpproxy?xm='+xm+'&sfzhm='+sfzhm+'&type='+type;
	//var url = 'error.json';
	//var url = 'empty.json';
	//var url = 'blData.json';
	//var url = 'mjData.json';
    $.ajax({
        type: "GET",
        url: url,
		contentType: 'application/json',
        dataType: "json",
        success: function (result) {
            ajaxSuccess(result,type);
        },
        error: function (result) {
			alert("服务器正在维护，请稍后在试");
        }
    });
}

//查询成功事件
function ajaxSuccess(result,type){
    if (result.code == 200) {
        var resultData = result.data;
		if(resultData == null || resultData == ''){
			$('.success_query').hide();
			$('.result').show();
			$('.error_query').show();
			return;
		}
        $('.result').show();
        $('.success_query').show();
		$('.error_query').hide();
		if(type == 'qzys'){ //确诊或者疑似
			loadBlData(resultData);
		}else if(type == 'mjrq'){
			if(result.isContactPerson == '1'){ //密切接触者
				loadMjData(resultData);
			}else if(result.isContactPerson == '0'){ //非密切接触者
				loadMjData(null);
			}
		}
    } else {
        $('.success_query').hide();
        $('.result').show();
        $('.error_query').show();
		alert(result.message);
    }
}

//加载病例数据
function loadBlData(resultData){
	var html = "";
	var xm = resultData.xm;
	var sfzhm = resultData.sfzhm;
	var bllx = resultData.bllx;
	var fbsj = resultData.fbsj;
	var qzsj = resultData.qzsj;
	html += '<table><thead><tr><th colspan="1" style="text-align:left;padding: 8px;">'+xm+'（'+sfzhm+'）'+'</th></tr></thead>'
		+ '<tbody>'
		+ '<tr><td>病历类型：'+bllx+'</td></tr>'
		+ '<tr><td>发病时间：'+fbsj+'</td></tr>'
		+ '<tr><td>确诊时间：'+qzsj+'</td></tr>'
		+ '</tbody>'
		+ '</table>';
	$('.success_query').html(html);
}

//加载密切接触者数据
function loadMjData(resultData) {
    var html = "";
	if(resultData == null){ //非密切接触者
		html += '<table><thead><tr><th colspan="1" style="text-align:left;padding: 8px;">非密切接触者</th></tr></thead></table>';
	}else{ //密切接触者
		if(resultData.length == 0){
			$('.success_query').hide();
			$('.result').show();
			$('.error_query').show();
			return;
		}
		for(var i=0;i<resultData.length;i++){
			var xm = resultData[i].xm;
			var sfzhm = resultData[i].sfzhm;
			var jclx = resultData[i].jclx;
			var jcrq = resultData[i].jcrq;
			html += '<table><thead><tr><th colspan="1" style="text-align:left;padding: 8px;">'+xm+'（'+sfzhm+'）'+'</th></tr></thead>'
				+ '<tbody>'
				+ '<tr><td>是否为密切接触者：<font color="red" weight=bold>是</font></td></tr>'
				+ '<tr><td>接触类型：'+jclx+'</td></tr>'
				+ '<tr><td>接触时间：'+jcrq+'</td></tr>'
				+ '</tbody>'
				+ '</table>';
		} 
	}
	$('.success_query').html(html);
}

//清空数据
function reset(){
	$('#xm').val('');
	$('#sfzhm').val('');
	$('.result').hide();
	$('.success_query').hide();
	$('.error_query').hide();
}